package com.linkage.seapeak.activity.project;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Project.CreateProjectDetailThreeActivity;
import com.linkage.jz.bankruptcy.base.BaseCreateActivity;
import com.linkage.jz.bankruptcy.bean.ProCrDecReviewInfo;
import com.linkage.jz.bankruptcy.bean.ZQBaseProjectCrDecInfo;
import com.linkage.jz.bankruptcy.bean.ZQBaseTagInfo;
import com.linkage.jz.bankruptcy.bean.ZQUserDetailBaseInfo;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.utils.LogUtil;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.seapeak.base.BaseFragment;
import com.linkage.seapeak.fragment.ExamineAddDataFragment;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 填写审查意见--- 一般债权人 或者  税务机关债券人
 */
public class EditCreditorReviewInfoActivity extends BaseCreateActivity {

    public static final String INTENT_IS_FIRST_EDIT       = "isFirstEdit";//true:初次填写  false:重新填写
    public static final String INTENT_IS_TAX_MODE         = "isTaxMode";//true：税务机关；false：债券一般债券人
    public static final String INTENT_IS_MANAGE_TYPE      = "IsManageType";//true:管理人 false：债权人
    public static final String INTENT_IS_REPLENISH_REVIEW = "isReplenishOrReviewPage";//true:补充 或 重新审查 类型

    public static final String INTENT_ID_PROJECT         = "ProjectId";
    public static final String INTENT_ID_CREDITOR_RIGHTS = "CreditorRightsId";
    public static final String INTENT_ID_REVIEW          = "ReviewId";
    public static final String INTENT_REVIEW_STATUS      = "ReviewStatus";
    public static final String INTENT_REEDIT_DATA        = "ReEditData";//重新审查填写数据
    public static final String INTENT_ID_EDIT      = "editData"; //修改审查


    private BaseFragment mBaseFragment;

    private String mProjectId;
    private String mCreditorRightsId;
    private String mReviewId;
    private String mReEditData;//重新编辑数据

    private boolean isFirstEdit;//true:初次填写  false:重新填写
    private boolean isManageType;//true:管理人 false：债权人
    private boolean isReplenishOrReviewPage;//补充 或 重新审查 类型
    private boolean isTaxMode;//true：一般债券人；false：税务机关债券

    private int sendStatus;//0--暂存，1--提交生成
    private boolean iseditData;//true 修改审查意见，false：非修改

    /**
     * 债权人:
     * 无菜单
     * 管理人:
     * 审查意见已发送 - 菜单只有『生成图片』 （reviewStatus =1
     * 审查意见未发送 - 菜单是『修改审查意见』和『生成图片』 （reviewStatus =3）
     */
    private int mReviewStatus;

    private PublicNoticeDialog mNoticeDialog;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        setContentView(R.layout.activity_examine_review_page);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

    }

    @Override
    public void initView() {

    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            isFirstEdit = intent.getBooleanExtra(INTENT_IS_FIRST_EDIT, true);
            isManageType = intent.getBooleanExtra(INTENT_IS_MANAGE_TYPE, true);
            isReplenishOrReviewPage = intent.getBooleanExtra(INTENT_IS_REPLENISH_REVIEW, false);
            isTaxMode = intent.getBooleanExtra(INTENT_IS_TAX_MODE, false);
            mProjectId = intent.getStringExtra(INTENT_ID_PROJECT);
            mCreditorRightsId = intent.getStringExtra(INTENT_ID_CREDITOR_RIGHTS);
            mReviewId = intent.getStringExtra(INTENT_ID_REVIEW);
            mReviewStatus = intent.getIntExtra(INTENT_REVIEW_STATUS, 0);
            mReEditData = intent.getStringExtra(INTENT_REEDIT_DATA);
            iseditData = intent.getBooleanExtra(INTENT_ID_EDIT,false);
            titlebar_menu_text.setVisibility(View.VISIBLE);
            titlebar_menu_text.setOnClickListener(view -> {
                if(isFirstEdit && !iseditData) {
                    mNoticeDialog = new PublicNoticeDialog(EditCreditorReviewInfoActivity.this, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {


                            if (view.getId() == R.id.ll_close) {//关闭
                                if (mNoticeDialog != null && mNoticeDialog.isShowing())
                                    mNoticeDialog.dismiss();
                                onBackPressed();

                            } else if (view.getId() == R.id.btn_chancel_notice) {

                                sendStatus = 0;
                                int sendStatustmp = 0;
                                if (mBaseFragment != null && mBaseFragment instanceof ExamineAddDataFragment) {
                                    final ExamineAddDataFragment fragment = (ExamineAddDataFragment) mBaseFragment;
                                    if (isFirstEdit) {
                                        fragment.sendReviewData(sendStatustmp);
                                    } else {
                                        fragment.sendReReviewData(sendStatustmp);
                                    }
                                    fragment.setOnSendNotifyListener(new ExamineAddDataFragment.onSendNotifyListener() {
                                        @Override
                                        public void onSendSuccess() {
                                            if (mNoticeDialog != null && mNoticeDialog.isShowing())
                                                mNoticeDialog.dismiss();

                                            fragment.onLocalDataSave();
                                            onBackPressed();

                                        }

                                        @Override
                                        public void onSendFailure() {
                                            if (mNoticeDialog != null && mNoticeDialog.isShowing())
                                                mNoticeDialog.dismiss();

                                            fragment.onLocalDataSave();

                                            onBackPressed();
                                        }
                                    });
                                }


                            } else if (view.getId() == R.id.btn_save_notice) {//生成
                                if (mBaseFragment != null && mBaseFragment instanceof ExamineAddDataFragment) {
                                    ExamineAddDataFragment fragment = (ExamineAddDataFragment) mBaseFragment;
                                    sendStatus = 1;
                                    if (isFirstEdit) {
                                        fragment.sendReviewData(sendStatus);
                                    } else {
                                        fragment.sendReReviewData(sendStatus);
                                    }
                                    fragment.setOnSendNotifyListener(new ExamineAddDataFragment.onSendNotifyListener() {
                                        @Override
                                        public void onSendSuccess() {
                                            onSendSuccessJumpDetail();
                                        }

                                        @Override
                                        public void onSendFailure() {

                                        }
                                    });
                                }
                            }
                        }
                    }, 1, "提示", "生成审查意见？", "暂存", "生成");
                    mNoticeDialog.show();
                }else{
                    sendStatus = 0;
                    int sendStatustmp = 0;
                    if (mBaseFragment != null && mBaseFragment instanceof ExamineAddDataFragment) {
                        final ExamineAddDataFragment fragment = (ExamineAddDataFragment) mBaseFragment;
                        if (isFirstEdit) {
                            fragment.sendReviewData(sendStatustmp);
                        } else {
                            fragment.sendReReviewData(sendStatustmp);
                        }
                        fragment.setOnSendNotifyListener(new ExamineAddDataFragment.onSendNotifyListener() {
                            @Override
                            public void onSendSuccess() {
                                if (mNoticeDialog != null && mNoticeDialog.isShowing())
                                    mNoticeDialog.dismiss();

                                fragment.onLocalDataSave();
                                onBackPressed();

                            }

                            @Override
                            public void onSendFailure() {
                                if (mNoticeDialog != null && mNoticeDialog.isShowing())
                                    mNoticeDialog.dismiss();

                                fragment.onLocalDataSave();

                                onBackPressed();
                            }
                        });
                    }
                }


            });
            onChangePageStatus();
            // 特殊处理下，
            titlebar_back.setOnClickListener(view -> onExitListener());
            if (isReplenishOrReviewPage) {
                titlebar_title.setText("补充审查意见");
            } else {
                titlebar_title.setText("审查意见");
            }
        }
    }

    @Override
    public void setListener() {

    }

    public void onChangePageStatus() {
        ExamineAddDataFragment fragment = ExamineAddDataFragment.getInstance(isTaxMode, isReplenishOrReviewPage, mProjectId, mReviewId, mCreditorRightsId);
        if (!isFirstEdit) {
            fragment.setFirstEdit(isFirstEdit);
            if (!TextUtils.isEmpty(mReEditData))
                fragment.setReEditData(mReEditData);
        }
        mBaseFragment = fragment;
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content, mBaseFragment).commitAllowingStateLoss();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {


        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            exit();
//            return false;
            onExitListener();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void onExitListener() {

        if(isFirstEdit  && ! iseditData) {
            mNoticeDialog = new PublicNoticeDialog(EditCreditorReviewInfoActivity.this, view -> {
                if (view.getId() == R.id.btn_chancel_notice) {//关闭
                    if (mNoticeDialog != null && mNoticeDialog.isShowing())
                        mNoticeDialog.dismiss();
                    onBackPressed();

                } else if (view.getId() == R.id.ll_close) {

                    finish();
                    return;
                } else if (view.getId() == R.id.btn_save_notice) {//暂存
                    sendStatus = 0;
                    int sendStatustmp = 0;
                    if (mBaseFragment != null && mBaseFragment instanceof ExamineAddDataFragment) {
                        final ExamineAddDataFragment fragment = (ExamineAddDataFragment) mBaseFragment;
                        if (isFirstEdit) {
                            fragment.sendReviewData(sendStatustmp);
                        } else {
                            fragment.sendReReviewData(sendStatustmp);
                        }
                        fragment.setOnSendNotifyListener(new ExamineAddDataFragment.onSendNotifyListener() {
                            @Override
                            public void onSendSuccess() {
                                if (mNoticeDialog != null && mNoticeDialog.isShowing())
                                    mNoticeDialog.dismiss();

                                fragment.onLocalDataSave();
                                onBackPressed();

                            }

                            @Override
                            public void onSendFailure() {
                                if (mNoticeDialog != null && mNoticeDialog.isShowing())
                                    mNoticeDialog.dismiss();

                                fragment.onLocalDataSave();

                                onBackPressed();
                            }
                        });
                    }
                }

            }, 1, "提示", "暂存编辑？", "否", "是");
            mNoticeDialog.show();
        }else{

            mNoticeDialog = new PublicNoticeDialog(EditCreditorReviewInfoActivity.this, view -> {
                if (view.getId() == R.id.btn_chancel_notice) {//关闭
                    if (mNoticeDialog != null && mNoticeDialog.isShowing())
                        mNoticeDialog.dismiss();
//                    onBackPressed();

                } else if (view.getId() == R.id.ll_close) {


                    return;
                } else if (view.getId() == R.id.btn_save_notice) {//暂存
                    finish();
                                if (mNoticeDialog != null && mNoticeDialog.isShowing())
                                    mNoticeDialog.dismiss();


                }

            }, 1, "提示", "退出编辑？", "否", "是");
            mNoticeDialog.show();

        }

//        mNoticeDialog = new PublicNoticeDialog(EditCreditorReviewInfoActivity.this, view -> {
//            if (view.getId() == R.id.btn_chancel_notice) {//关闭
//                sendStatus = 0;
//            } else if (view.getId() == R.id.ll_close) {
//                if (mNoticeDialog != null && mNoticeDialog.isShowing())
//                    mNoticeDialog.dismiss();
//                onBackPressed();
//                return;
//            } else if (view.getId() == R.id.btn_save_notice) {//暂存
//                sendStatus = 1;
//            }
//            if (mBaseFragment != null && mBaseFragment instanceof ExamineAddDataFragment) {
//                final ExamineAddDataFragment fragment = (ExamineAddDataFragment) mBaseFragment;
//                if (isFirstEdit) {
//                    fragment.sendReviewData(sendStatus);
//                } else {
//                    fragment.sendReReviewData(sendStatus);
//                }
//                fragment.setOnSendNotifyListener(new ExamineAddDataFragment.onSendNotifyListener() {
//                    @Override
//                    public void onSendSuccess() {
//                        if (mNoticeDialog != null && mNoticeDialog.isShowing())
//                            mNoticeDialog.dismiss();
//                        if (sendStatus == 0) {
//                            fragment.onLocalDataSave();
//                            onBackPressed();
//                        } else if (sendStatus == 1) {
//                            onSendSuccessJumpDetail();
//                        }
//                    }
//
//                    @Override
//                    public void onSendFailure() {
//                        if (mNoticeDialog != null && mNoticeDialog.isShowing())
//                            mNoticeDialog.dismiss();
//                        if (sendStatus == 0) {
//                            fragment.onLocalDataSave();
//                        }
//                        onBackPressed();
//                    }
//                });
//            }
//        }, 1, "提示", "暂存编辑？", "否", "是");
//        mNoticeDialog.show();
    }

    private void onSendSuccessJumpDetail() {
        onBackPressed();
        if (isFirstEdit) {
            if (mBaseFragment != null && mBaseFragment instanceof ExamineAddDataFragment) {
                final ExamineAddDataFragment fragment = (ExamineAddDataFragment) mBaseFragment;
                Intent                       intent   = new Intent(this, CreditorReviewDetailActivity.class);
                intent.putExtra(CreditorReviewDetailActivity.INTENT_IS_MANAGE_TYPE, true);//todo 需要标识判断
                intent.putExtra(CreditorReviewDetailActivity.INTENT_IS_TAX_MODE, isTaxMode);//true：税务，false： //非税务
                intent.putExtra(CreditorReviewDetailActivity.INTENT_IS_REPLENISH_REVIEW, isReplenishOrReviewPage);//true:补充 类型
                intent.putExtra(CreditorReviewDetailActivity.INTENT_ID_PROJECT, mProjectId);
                intent.putExtra(CreditorReviewDetailActivity.INTENT_ID_REVIEW, fragment.getDataReviewId());
                intent.putExtra(CreditorReviewDetailActivity.INTENT_ID_CREDITOR_RIGHTS, fragment.getDataCreditorRightsId());
                intent.putExtra(CreditorReviewDetailActivity.INTENT_REVIEW_STATUS, 3);//todo 获取reviewStatus
                startActivity(intent);

            }
        } else {
            EventBus.getDefault().post("reSubmitInfo");
        }
    }



}
